﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using QuanLyRapChieuPhim.DTO;
using System.ComponentModel;


namespace QuanLyRapChieuPhim.DAO
{
    class SuatChieuDAO
    {
        DataProvider dp = new DataProvider();

        public BindingList<SuatChieuDTO> GetList()
        {

            DataTable dt = new DataTable();
            BindingList<SuatChieuDTO> ds = new BindingList<SuatChieuDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_SuatChieu", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    SuatChieuDTO SuatChieu = new SuatChieuDTO();
                    SuatChieu.MaSuatChieu = int.Parse(row["maSuatChieu"].ToString());
                    SuatChieu.TenSuatChieu = row["tenSuat"].ToString();
                    SuatChieu.MaCaChieu = int.Parse(row["maCaChieu"].ToString());
                    SuatChieu.MaPhim = int.Parse(row["maPhim"].ToString());
                    SuatChieu.SoLuongVe = int.Parse(row["soLuongVe"].ToString());
                    SuatChieu.TenPhim = row["tenPhim"].ToString();
                    SuatChieu.TenCaChieu = row["tenCaChieu"].ToString();
                    SuatChieu.MaPhongChieu = int.Parse(row["maPhongChieu"].ToString());
                    SuatChieu.NgayChieu = DateTime.Parse(row["NgayChieu"].ToString());
                    ds.Add(SuatChieu);
                }
                return ds;
            }
            return null;
        }

        public BindingList<SuatChieuDTO> GetList_QL()
        {

            DataTable dt = new DataTable();
            BindingList<SuatChieuDTO> ds = new BindingList<SuatChieuDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_SuatChieu_QL", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    SuatChieuDTO SuatChieu = new SuatChieuDTO();
                    SuatChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    SuatChieu.MaSuatChieu = int.Parse(row["maSuatChieu"].ToString());
                    SuatChieu.TenSuatChieu = row["tenSuat"].ToString();
                    SuatChieu.MaCaChieu = int.Parse(row["maCaChieu"].ToString());
                    SuatChieu.MaPhim = int.Parse(row["maPhim"].ToString());
                    SuatChieu.SoLuongVe = int.Parse(row["soLuongVe"].ToString());
                    SuatChieu.TenPhim = row["tenPhim"].ToString();
                    SuatChieu.TenCaChieu = row["tenCaChieu"].ToString();
                    SuatChieu.MaPhongChieu = int.Parse(row["maPhongChieu"].ToString());
                    SuatChieu.NgayChieu = DateTime.Parse(row["NgayChieu"].ToString());
                    ds.Add(SuatChieu);
                }
                return ds;
            }
            return null;
        }

        public BindingList<SuatChieuDTO> GetList_QL_DaXoa()
        {

            DataTable dt = new DataTable();
            BindingList<SuatChieuDTO> ds = new BindingList<SuatChieuDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_SuatChieu_QL_DaXoa", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    SuatChieuDTO SuatChieu = new SuatChieuDTO();
                    SuatChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    SuatChieu.MaSuatChieu = int.Parse(row["maSuatChieu"].ToString());
                    SuatChieu.TenSuatChieu = row["tenSuat"].ToString();
                    SuatChieu.MaCaChieu = int.Parse(row["maCaChieu"].ToString());
                    SuatChieu.MaPhim = int.Parse(row["maPhim"].ToString());
                    SuatChieu.SoLuongVe = int.Parse(row["soLuongVe"].ToString());
                    SuatChieu.TenPhim = row["tenPhim"].ToString();
                    SuatChieu.TenCaChieu = row["tenCaChieu"].ToString();
                    SuatChieu.MaPhongChieu = int.Parse(row["maPhongChieu"].ToString());
                    SuatChieu.NgayChieu = DateTime.Parse(row["NgayChieu"].ToString());
                    ds.Add(SuatChieu);
                }
                return ds;
            }
            return null;
        }

        public BindingList<SuatChieuDTO> GetList_FromRap(int _Ma)
        {

            DataTable dt = new DataTable();
            BindingList<SuatChieuDTO> ds = new BindingList<SuatChieuDTO>();
            SqlParameter[] sqlParameter = { new SqlParameter("@maRap", _Ma) };
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_SuatChieu_Rap", sqlParameter);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    SuatChieuDTO SuatChieu = new SuatChieuDTO();
                    SuatChieu.MaSuatChieu = int.Parse(row["maSuatChieu"].ToString());
                    SuatChieu.TenSuatChieu = row["tenSuat"].ToString();
                    SuatChieu.MaCaChieu = int.Parse(row["maCaChieu"].ToString());
                    SuatChieu.MaSuatChieu = int.Parse(row["maSuatChieu"].ToString());
                    SuatChieu.MaPhim = int.Parse(row["maPhim"].ToString());
                    SuatChieu.SoLuongVe = int.Parse(row["soLuongVe"].ToString());
                    SuatChieu.TenPhim = row["tenPhim"].ToString();
                    SuatChieu.TenCaChieu = row["tenCaChieu"].ToString();
                    SuatChieu.MaPhongChieu = int.Parse(row["maPhongChieu"].ToString());
                    SuatChieu.NgayChieu = DateTime.Parse(row["NgayChieu"].ToString());
                    ds.Add(SuatChieu);
                }
                return ds;
            }
            return null;
        }

        public BindingList<SuatChieuDTO> GetListLikeName_QL(String _Ten)
        {

            DataTable dt = new DataTable();
            BindingList<SuatChieuDTO> ds = new BindingList<SuatChieuDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ten", _Ten) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetListLikeName_SuatChieu_QL", sqlParameter);
                foreach (DataRow row in dt.Rows)
                {
                    SuatChieuDTO SuatChieu = new SuatChieuDTO();
                    SuatChieu.MaSuatChieu = int.Parse(row["maSuatChieu"].ToString());
                    SuatChieu.TenSuatChieu = row["tenSuat"].ToString();
                    SuatChieu.MaCaChieu = int.Parse(row["maCaChieu"].ToString());
                    SuatChieu.MaSuatChieu = int.Parse(row["maSuatChieu"].ToString());
                    SuatChieu.MaPhim = int.Parse(row["maPhim"].ToString());
                    SuatChieu.SoLuongVe = int.Parse(row["soLuongVe"].ToString());
                    SuatChieu.TenPhim = row["tenPhim"].ToString();
                    SuatChieu.TenCaChieu = row["tenCaChieu"].ToString();
                    SuatChieu.MaPhongChieu = int.Parse(row["maPhongChieu"].ToString());
                    SuatChieu.NgayChieu = DateTime.Parse(row["NgayChieu"].ToString());
                    ds.Add(SuatChieu);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public BindingList<SuatChieuDTO> GetListLikeName_QL_DaXoa(String _Ten)
        {

            DataTable dt = new DataTable();
            BindingList<SuatChieuDTO> ds = new BindingList<SuatChieuDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ten", _Ten) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetListLikeName_SuatChieu_QL_DaXoa", sqlParameter);
                foreach (DataRow row in dt.Rows)
                {
                    SuatChieuDTO SuatChieu = new SuatChieuDTO();
                    SuatChieu.MaSuatChieu = int.Parse(row["maSuatChieu"].ToString());
                    SuatChieu.TenSuatChieu = row["tenSuat"].ToString();
                    SuatChieu.MaCaChieu = int.Parse(row["maCaChieu"].ToString());
                    SuatChieu.MaSuatChieu = int.Parse(row["maSuatChieu"].ToString());
                    SuatChieu.MaPhim = int.Parse(row["maPhim"].ToString());
                    SuatChieu.SoLuongVe = int.Parse(row["soLuongVe"].ToString());
                    SuatChieu.TenPhim = row["tenPhim"].ToString();
                    SuatChieu.TenCaChieu = row["tenCaChieu"].ToString();
                    SuatChieu.MaPhongChieu = int.Parse(row["maPhongChieu"].ToString());
                    SuatChieu.NgayChieu = DateTime.Parse(row["NgayChieu"].ToString());
                    ds.Add(SuatChieu);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public SuatChieuDTO GetFromId(int _Ma)
        {
            DataTable dt = new DataTable();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@Ma", _Ma) };
                dt = dp.SqlExcuteQuery("uproc_GetFromID_SuatChieu", sqlParameter);
                dp.CloseConnect();
                SuatChieuDTO SuatChieu = new SuatChieuDTO();
                foreach (DataRow row in dt.Rows)
                {

                    SuatChieu.MaSuatChieu = int.Parse(row["maSuatChieu"].ToString());
                    SuatChieu.TenSuatChieu = row["tenSuat"].ToString();
                    SuatChieu.MaCaChieu = int.Parse(row["maCaChieu"].ToString());
                    SuatChieu.MaSuatChieu = int.Parse(row["maSuatChieu"].ToString());
                    SuatChieu.MaPhim = int.Parse(row["maPhim"].ToString());
                    SuatChieu.SoLuongVe = int.Parse(row["soLuongVe"].ToString());
                    SuatChieu.MaPhongChieu = int.Parse(row["maPhongChieu"].ToString());
                    SuatChieu.NgayChieu = DateTime.Parse(row["NgayChieu"].ToString());
                }
                return SuatChieu;
            }
            return null;
        }

        public int GetIDFromIDTTDat(int _Ma)
        {
            DataTable dt = new DataTable();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaTTDatVe", _Ma) };
                dt = dp.SqlExcuteQuery("uproc_IDSuatFromIDTTDatVe", sqlParameter);
                dp.CloseConnect();
                SuatChieuDTO SuatChieu = new SuatChieuDTO();
                if (dt.Rows.Count > 0)
                {
                    return int.Parse(dt.Rows[0][0].ToString());
                }
                dt = dp.SqlExcuteQuery("uproc_GetFirtID_SuatChieu", null);
                dp.CloseConnect();
                return int.Parse(dt.Rows[0][0].ToString());
            }
            return -1;
        }

        public bool Delete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Delete_SuatChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool Delete_HetHan()
        {
            if (dp.OpenConnect())
            {
                bool kq = dp.SqlExcuteNonQuery("uproc_Delete_SuatChieu_HetHan", null);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool UnDelete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_UnDelete_SuatChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool Add(SuatChieuDTO _DTO)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaCaChieu", _DTO.MaCaChieu), 
                    new SqlParameter("@MaPhim", _DTO.MaPhim),
                    new SqlParameter("@MaPhongChieu", _DTO.MaPhongChieu),
                    new SqlParameter("@SoLuongVe", _DTO.SoLuongVe) ,
                    new SqlParameter("@TenSuat", _DTO.TenSuatChieu) ,
                    new SqlParameter("@NgayChieu", _DTO.NgayChieu)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Add_SuatChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool CheckExists(int _Ma, string _Ten)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma), 
                    new SqlParameter("@TenSuatChieu", _Ten)
                };
                DataTable dt = dp.SqlExcuteQuery("uproc_CheckExists_SuatChieu", sqlParameter);
                dp.CloseConnect();
                if (dt.Rows.Count == 0)
                    return false;
                return true;// đã tồn tại
            }
            return false;
        }

        public bool Update(SuatChieuDTO _DTO)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaSuatChieu", _DTO.MaSuatChieu), 
                    new SqlParameter("@MaCaChieu", _DTO.MaCaChieu), 
                    new SqlParameter("@MaPhim", _DTO.MaPhim),
                    new SqlParameter("@MaPhongChieu", _DTO.MaPhongChieu),
                    new SqlParameter("@SoLuongVe", _DTO.SoLuongVe) ,
                    new SqlParameter("@TenSuat", _DTO.TenSuatChieu) ,
                    new SqlParameter("@NgayChieu", _DTO.NgayChieu)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Update_SuatChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public int DemVeDaBan(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@Ma", _Ma) };
                int re = int.Parse(dp.SqlExcuteScalar("uproc_DemVeDaBan_Suat", sqlParameter).ToString());
                dp.CloseConnect();
                return re;
            }
            return -1;
        }

    }
}
